package com.example.juc.threadPool.synchronizedTest;

import java.util.concurrent.TimeUnit;

public class TestSynchronized {

    public static void main(String[] args) {
        TestSynchronized test = new TestSynchronized();
        Thread t1 = new Thread(() ->{
            test.method("123",4);
        });

        Thread t2 = new Thread(() ->{
            test.method("456",4);
        });

        t1.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        t2.start();
    }



    public void method(String code,Integer time){
        synchronized (code.intern()){
            System.out.println("执行业务操作,当前线程名称："+Thread.currentThread().getName());
            if(time > 0){
                try {
                    TimeUnit.SECONDS.sleep(time);
                    System.out.println("任务执行完毕，当前线程名称："+Thread.currentThread().getName());
                }catch (Exception ignored){
                }

            }
        }

    }

}
